﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using System.Configuration;

namespace Common
{
    public class Logger
    {
        private bool EnableLogging = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableLogging"]);
        public Logger()
        {
        }

        /// <summary>
        /// Запись в Лог Exception с внутренними InnerException
        /// </summary>
        /// <param name="ex"></param>
        public void LogException(Exception ex)
        {
            if (EnableLogging)
            {
                var logger = LogManager.GetCurrentClassLogger();

                var x = ex;
                do
                {
                    logger.LogException(LogLevel.Error, "ERROR: ", x);
                    x = x.InnerException;
                } while (x != null);
            }
        }


        /// <summary>
        /// Логирование текстового сообщения
        /// </summary>
        /// <param name="message"></param>
        public void LogMessage(string message)
        {
            if (EnableLogging)
            {
                var logger = LogManager.GetCurrentClassLogger();
                logger.Info(string.Format("Информационное сообщение: \n\r{0}", message));
            }
        }


    }
}
